package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Dept;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.service.DeptService;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.ldap.PagedResultsControl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 21/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequiresPermissions("dept:ajax")
    @RequestMapping("/resouce/demo2/roleList")
    @ResponseBody
    public List<Role> findRoleList(Integer did){
        List<Role> roleList = roleService.findRoleList(did);
     return roleList;
    }

//    @RequiresPermissions("role:findall")
    @RequestMapping("/resouce/demo3/pageShow")
    public String showPage(@RequestParam(defaultValue ="1") Integer index, String rname, String dept, Integer rstate, ModelMap map, HttpServletRequest request,Integer[] roleids,HttpServletResponse response){
        String method = request.getParameter("method");
        if (method==null||"".equals(method)){
            PageInfo<Role> pageInfo = roleService.showPage(index, rname, dept, rstate);
            map.addAttribute("info",pageInfo);
            map.addAttribute("rname",rname);
            map.addAttribute("dept",dept);
            map.addAttribute("state",rstate);
            return "resource/demo3/list";
        }else {
            Integer i= roleService.deleteByRoleIds(roleids);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = null;
            try {
                writer = response.getWriter();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (i==-1){
                writer.print("<script>alert('此角色仍存在其他表中,无法删除');location.href='/resouce/demo3/pageShow';</script>");
            }else{
                writer.print("<script>alert('删除成功');location.href='/resouce/demo3/pageShow';</script>");
            }
            return null;
        }
    }
    @Autowired
    private MenuService menuService;

    @Autowired
    private DeptService deptService;

//    @RequiresPermissions("role:menu")
    @RequestMapping("/resouce/demo3/showMenuList")
    public String showMenuList(ModelMap map){
        List<Menu> menus=menuService.findMenu();
        List<Dept> deptList = deptService.findDeptList();
        map.addAttribute("menus",menus);
        map.addAttribute("depts",deptList);
        return "resource/demo3/add";
    }

    @RequestMapping("/resouce/demo3/add")
//    @RequiresPermissions("role:add")
    public void addRole(Role role, HttpServletResponse response){
       Integer i= roleService.addRole(role);
       response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (i>0){
            writer.print("<script>alert('添加成功');location.href='/resouce/demo3/pageShow';</script>");
       }else{
            writer.print("<script>alert('添加失败');location.href='/resource/demo3/add.jsp';</script>");
        }
    }

//    @RequiresPermissions("role:showback")
    @RequestMapping("/resouce/demo3/showBack")
    public String updateShowBack(Integer roleid,ModelMap map){
        Role role=roleService.updateShowBack(roleid);
        map.addAttribute("role",role);
        List<Menu> menus = menuService.findMenu();
        map.addAttribute("menus",menus);
        List<Dept> deptList = deptService.findDeptList();
        map.addAttribute("depts",deptList);
        return "resource/demo3/update";
    }


//    @RequiresPermissions("role:update")
    @RequestMapping("/resouce/demo3/update")
    public void update(Role role,HttpServletResponse response){
        System.out.println(role+"==================================");
        response.setContentType("text/html;charset=utf-8");
        Integer a=roleService.update(role);
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (a>0){
            writer.print("<script>alert('修改成功');location.href='/resouce/demo3/pageShow';</script>");
        }else{
            writer.print("<script>alert('修改失败');location.href='/resouce/demo3/pageShow';</script>");
        }

    }


//    @RequiresPermissions("role:deleteid")
    @RequestMapping("/resouce/demo3/deleterole")
    public void deleterole (Integer roleid,HttpServletResponse response){
        Integer i=roleService.deleterole(roleid);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (i==-1){
            writer.print("<script>alert('此角色仍存在其他表中');location.href='/resouce/demo3/pageShow';</script>");
        }else{
            writer.print("<script>alert('删除成功');location.href='/resouce/demo3/pageShow';</script>");
        }

    }


}
